<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/common/CommonEntryView.php';
require_once APP_CLIENT_DIR . 'client/inc/KBClientController.php';


class KBEntryView_list extends AppView
{
	
	var $template = 'list.html';
	var $template_popup = 'list_popup.html';
	
	
	function execute(&$obj, &$manager) {
		
		$tmpl = ($this->controller->getMoreParam('popup')) ? $this->template_popup :  $this->template;
		$add_button = ($this->controller->getMoreParam('popup')) ? false :  true;
		
		$client_controller = &new KBClientController();
		$client_controller->kb_path = APP_CLIENT_PATH;
		$client_controller->mod_rewrite = false;
		
		
		$tpl = &new tplTemplatez($this->template_dir . $tmpl);
		$tpl->tplSetNeededGlobal('attachment');
		
		
		// setting/fields 
		$s = &new SettingModel;
		$s->module_id = 100; 
		$setting = &$s->getSettings();
		if($setting['allow_comments']) { $tpl->tplSetNeededGlobal('comment'); } 
		if($setting['allow_rating'])   { $tpl->tplSetNeededGlobal('rating'); } 

		
		// filter sql
		$categories = $manager->getCategoryRecords();
		$manager->setSqlParams($this->getFilterSql($manager, $categories));
		
		// header generate
		$bp = &$this->pageByPage($manager->limit, $manager->getCountRecords());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav, $this->getFilter($manager, $categories), $add_button));
		
		// sort generate
		$sort = &$this->getSort();
		$manager->setSqlParamsOrder($sort->getSql());

		// get records
		$rows = &$this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		$ids = $manager->getValuesString($rows, 'id'); 	
		
		// categories
		$categories = ($ids) ? $manager->getCategoryByIds($ids) : array();
		$categories = &$this->stripVars($categories);
		
		// roles to entry
		$tpl->tplSetNeededGlobal('private');
		if($manager->isExtra('private')) {
			
			$roles = ($ids) ? $manager->getMemberRoleById($ids, true) : array();
			$roles = &$this->stripVars($roles);
			
			$ids = $manager->getValuesString($rows, 'category_id'); 
			$category_roles = ($ids) ? $manager->cat->getMemberRoleById($ids, true) : array();
			$category_roles = &$this->parseCategoryRoles($category_roles);		
		}

		
		// list records
		foreach($rows as $row) {
			
			$obj->set($row);
			
			$tpl->tplAssign('escaped_title', addslashes($obj->get('title')));
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplAssign('category', implode('<br>', $categories[$row['id']]));
            if($row['middle_name']) {
               $tpl->tplAssign('author', ucwords($row['last_name'] .', '.$row['first_name'].' '.$row['middle_name']));
            } else {
                $tpl->tplAssign('author', ucwords($row['last_name'].' '.$row['first_name']));
            }

//            $tpl->tplAssign('author',$row['author_name']);
			$tpl->tplAssign('comment_num', $row['comment_num']);
			$tpl->tplAssign('attachment_num', $row['attachment_num']);
			$tpl->tplAssign('votes', $row['votes']);
			$tpl->tplAssign('rating_img', $this->getRatingImg($row['rating'], $row['votes']));
			
			$entry_link = $client_controller->getLink('entry', $row['category_id'], $obj->get('id'));
			$tpl->tplAssign('entry_link', $entry_link);
			
			// private&roles
			//if($row['private'] || $row['category_private'] && $manager->isExtra('private')) {
			if($row['private'] || $row['category_private']) {
				$tpl->tplAssign('roles_msg', $this->getPrivateMsg(@$roles[$row['id']], @$category_roles[$row['category_id']]));
				$tpl->tplAssign($this->getColorsAndRolesMsg($row));
				$tpl->tplSetNeeded('row/if_private');
			}
			
			//$active = (!$row['category_active']) ? 'none' : true;
			$tpl->tplAssign($this->getViewListVars($obj->get('id'), $obj->get('active')));
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
/*
	function getViewListVars($id = false, $active = false, $category_id = false, $cont) {
		
		$row = parent::getViewListVars($id, $active);
		
		$client_link = array('View' => 'entry', 'CategoryID' => $category_id, 'EntryID' => $id);
		$row ['entry_link'] = $this->controller->getLink('knowledgebase', 'kb_client', false, false, $client_link);
		
		return $row;
	}
*/
	
	
	function &parseCategoryRoles($category_roles) {
		return CommonEntryView::parseCategoryRoles($this->stripVars($category_roles), $this->msg);
	}
	
	
	function getPrivateMsg($entry_roles, $category_roles) {
		return CommonEntryView::getPrivateMsg($entry_roles, $category_roles);
	}
	
	
	function getColorsAndRolesMsg($row) {
		return CommonEntryView::getColorsAndRolesMsg($row, $this->msg);
	}
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 2;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('votes_num_msg',  'votes', 'votes', $this->msg['votes_num_msg']);
		$sort->setSortItem('rating_msg',  'rating', 'rating', $this->msg['rating_msg']);
		$sort->setSortItem('entry_title_msg',  'title', 'title', $this->msg['entry_title_msg']);
		$sort->setSortItem('comment_num_msg', 'comments', 'comment_num', $this->msg['comment_num_msg']);
		$sort->setSortItem('attachment_num_msg', 'attachments', 'attachment_num', $this->msg['attachment_num_msg']);		
		//$sort->setSortItem('private_msg', 'private', 'e.private', array($this->msg['private_msg'], 1));
		$sort->setSortItem('hits_num_msg', 'hits', 'hits', $this->msg['hits_num_msg']);
		
		//$sort->setSortItem('sort_order_msg', 'sort_oder', 'sort_order', $this->msg['sort_order_msg'], 1);
		$sort->setSortItem('date_posted_msg', 'date', 'date_posted', $this->msg['date_posted_msg']);
		$sort->setSortItem('status_msg','status', 'active', $this->msg['status_msg']);
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function getFilter($manager, $categories) {

		$tpl = &new tplTemplatez($this->template_dir . 'form_filter.html');
	
		$select = new FormSelect();
		$select->select_tag = false;
		//$select->setSelectName('category');
		$select->setRange($manager->getCategorySelectRange($categories), 
		                  array('all'=>$this->msg['all_categories_msg']));	

		@$category_id = $_GET['filter']['c'];
		$tpl->tplAssign('category_select', $select->select($category_id));
		
		$tpl->tplAssign($this->setCommonFormVarsFilter());
		$tpl->tplAssign($this->msg);
		
		$tpl->tplParse(@$_GET['filter']);
		return $tpl->tplPrint(1);
	}
	
	
	function getFilterSql($manager, $categories) {
		
		// filter
		$arr = array();
		
		@$v = $_GET['filter']['c'];
		if($v != 'all' && !empty($v)) {
			
			$category_id = (int) $v;
			$child = array_merge($manager->getChilds($categories, $category_id), array($category_id));
			$child = implode(',', $child);
			
			$arr[] = "AND e_to_cat.category_id IN($child)";
		}
		
		
		@$v = $_GET['filter']['q'];
		if($v != 'all' && !empty($v)) {
			
			$v = addslashes($v);
			$arr[] = "AND e.title LIKE '{$v}%'";
		}		
		
		return implode(" \n", $arr);
	}
	
	
	function getRatingImg($rate, $votes) {
		$str = '<img src="images/rating/rate_line_8px/rate_%s.gif" alt="" border="0">';
		$rate = round($rate);
		return ($votes) ? sprintf($str, $rate) : '';
	}
}
?>